﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.vcode;
namespace com.fukefu.info.b2b.byhand
{
    public class QQYY : Information
    {
        string code_url = "http://u.qqyy.com/tools/ValidateCode.aspx?m=11";
        VCODE code = null;

        public override bool Login(string name, string pass, string vcode)
        {
            code = new VCODE(site);
            vcode = GetFlushCodeImage2();

            ParametersData pd = new ParametersData();
            pd.Add("action", "login");
            pd.Add("username", name);
            pd.Add("loginpass", pass);
            pd.Add("verifycode", vcode);
            pd.Add("jzdl", "false");

            string html = source.Post("http://u.qqyy.com/handler/LoginR.ashx", pd.Read(), "http://www.qqyy.com/");
            if (html.Equals("true"))
            {
                code_url = "http://u.qqyy.com/tools/ValidateCode1.aspx?m=11";
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            vcode = GetFlushCodeImage2();
            InformationResult info = new InformationResult();
            content = Regex.Replace(content, "<p>\\s*</p>", "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, "<br/?>", "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, "<p>　　", "<P>", RegexOptions.IgnoreCase);
            string html = source.Get("http://u.qqyy.com/hosp/NewsAdd.aspx", "http://u.qqyy.com/hosp/NewsList.aspx");
            if (html.IndexOf("当日文章更新量已满") != -1)
            {
                info.Code = 3;
                return info;
            }
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)").Groups[1].Value);
            sfd.Write("__EVENTVALIDATION", Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)").Groups[1].Value);
            sfd.Write("txtTitle", title);
            sfd.Write("txtID", "0");
            //发送文件
            sfd.WriteFile("fileImgUrl", "");
            //尾部数据
            sfd.Write("txtImgUrl", "");
            sfd.Write("selArcType", site.TypeId[0]);
            sfd.Write("txtSelType", "");
            sfd.Write("dropAdmOffId", "0");
            sfd.Write("txtKeyWord", title);
            sfd.Write("txtSeoDec", title);
            sfd.Write("txtContent", content);
            sfd.Write("txtCode", vcode);
            sfd.Write("btnTypeSub", "提交");
            //结尾
            html = source.Post("http://u.qqyy.com/hosp/NewsAdd.aspx", sfd.GetContentType(), sfd.Read(), "http://u.qqyy.com/hosp/NewsAdd.aspx");
            if (html.IndexOf("confirm('添加成功，是否还要添加") != -1)
            {
                html = source.Get("http://u.qqyy.com/hosp/NewsList.aspx");
                if (Regex.IsMatch(html, "<a href=\"(http://hospital.qqyy.com/[^\"]+)\" target=\"_blank\">" + title + "</a>"))
                {
                    info.Code = 1;
                    info.Content = Regex.Match(html, "<a href=\"(http://hospital.qqyy.com/[^\"]+)\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
                }
            }
            if (Regex.IsMatch(html, "<div class=\"error_item\">\\s+<h4>([^<]+)</h4>"))
            {
                info.Content = Regex.Match(html, "<div class=\"error_item\">\\s+<h4>([^<]+)</h4>").Groups[1].Value;
            }
            if (Regex.IsMatch(html, "<script> alert\\('([^\']+)'\\)"))
            {
                info.Content = Regex.Match(html, "<script> alert\\('([^\']+)'\\)").Groups[1].Value;
            }
            return info;
        }
        public string GetFlushCodeImage2()
        {
            return code.Get(source.GetStream(code_url, "http://u.qqyy.com/hosp/NewsAdd.aspx"));
        }
        public override Image GetFlushCodeImage()
        {
            return source.GetImage(code_url, "http://u.qqyy.com/hosp/NewsAdd.aspx");
        }
    }
}
